package imp;

import dao.CollectionDao;
import model.Collection;
import model.Comments;
import model.InformAgainst;
import util.DUBtil;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Collection_imp implements CollectionDao {
    @Override
    public boolean AddCollection(Collection collection) {
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        String sql = "insert into collection(id,userid,goodsid,date) values(?,?,?,?)";
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, collection.getId());
            pstmt.setString(2, collection.getUserid());
            pstmt.setString(3, collection.getGoodsid());
            pstmt.setString(4, collection.getDate());
            pstmt.executeUpdate();
            con.close();
            pstmt.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Collection judgeinfo(String GoodsId, String UserId) {
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Collection collection = new Collection();
        String sql = "select * from collection where goodsid=? and userid=?";
        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, GoodsId);
            pstmt.setString(2, UserId);
            System.out.println("交易记录查找SQL语句:" + pstmt);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                collection.setId(rs.getString("id"));
            }
            rs.close();
            pstmt.close();
            con.close();
            return collection;
        } catch (SQLException e) {
            e.printStackTrace();
            return collection;
        }
    }

    @Override
    public List<Collection> ShowCollection(String id) {
        Connection con = new DUBtil().getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select co.id,co.userid,co.goodsid,co.date,go.imgurl,go.name\n" +
                "from collection co,goods go\n" +
                "where co.goodsid=go.id and co.userid=? ORDER BY date DESC";
        List<Collection> list = new ArrayList<>();

        try {
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, id);
            rs = pstmt.executeQuery();
            System.out.println("显示收藏SQL代码:"+pstmt);
            while (rs.next()) {
                Collection collection = new Collection();
                collection.setId(rs.getString("id"));
                collection.setUserid(rs.getString("userid"));
                collection.setGoodsid(rs.getString("goodsid"));
                collection.setDate(rs.getString("date"));
                collection.setGoodsimgurl(rs.getString("imgurl"));
                collection.setGoodsname(rs.getString("name"));
                list.add(collection);
            }
            rs.close();
            pstmt.close();
            con.close();
            return list;
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null,
                    "数据库访问失败");
            return null;
        }
    }

    @Override
    public boolean DeleteCollection(String id) {
        Connection connection = new DUBtil().getConnection();
        PreparedStatement pstml = null;
        String sql = "delete from collection where id=?";
        try {
            pstml = connection.prepareStatement(sql);
            pstml.setString(1, id);
            pstml.executeUpdate();
            connection.close();
            pstml.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}
